package com.abc.recycling.controller;

import com.abc.recycling.model.RecyclingStation;
import com.abc.recycling.service.RecyclingStationService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Controller
@RequestMapping("/collector/map")
@RequiredArgsConstructor
public class CollectorMapController {

    private final RecyclingStationService stationService;

    @GetMapping("")
    public String showMap(
            @RequestParam(required = false) Double lat,
            @RequestParam(required = false) Double lng,
            @RequestParam(required = false) String address,
            Model model) {
        try {
            log.info("显示回收站地图页面");
            // 获取所有营业中的回收站
            Pageable pageable = PageRequest.of(0, Integer.MAX_VALUE);
            Page<RecyclingStation> stationsPage = stationService.findAll(pageable);
            model.addAttribute("stations", stationsPage.getContent());
            
            // 如果提供了坐标和地址，添加到模型中
            if (lat != null && lng != null) {
                log.debug("设置目标位置: lat={}, lng={}", lat, lng);
                model.addAttribute("targetLat", lat);
                model.addAttribute("targetLng", lng);
            }
            if (address != null) {
                model.addAttribute("address", address);
            }
            
            return "collector/map";
        } catch (Exception e) {
            log.error("显示地图页面失败: {}", e.getMessage());
            throw e;
        }
    }

    @GetMapping("/content")
    public String getMapContent(
            @RequestParam(required = false) Double lat,
            @RequestParam(required = false) Double lng,
            Model model) {
        // 获取所有营业中的回收站
        Pageable pageable = PageRequest.of(0, Integer.MAX_VALUE);
        Page<RecyclingStation> stationsPage = stationService.findAll(pageable);
        model.addAttribute("stations", stationsPage.getContent());
        
        // 如果提供了坐标，添加到模型中
        if (lat != null && lng != null) {
            model.addAttribute("targetLat", lat);
            model.addAttribute("targetLng", lng);
        }
        
        return "collector/map :: mapContent";
    }

    @GetMapping("/stations")
    public String getStations(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size,
            Model model) {
        Pageable pageable = PageRequest.of(page, size);
        model.addAttribute("stations", stationService.findAll(pageable));
        return "collector/stations";
    }

    @GetMapping("/nearby")
    public ResponseEntity<List<RecyclingStation>> getNearbyStations(
            @RequestParam Double latitude,
            @RequestParam Double longitude,
            @RequestParam Integer radius) {
        return ResponseEntity.ok(stationService.getNearbyStations(latitude, longitude, radius));
    }
} 